package com.yang.service;

import com.yang.dao.BasicInfoDao;
import com.yang.dao.InfoDetailDao;
import com.yang.pojo.BasicInfo;
import com.yang.pojo.InfoDetail;

import java.sql.SQLException;
import java.util.ArrayList;

public class BasicInfoService {
    BasicInfoDao basicInfoDao = new BasicInfoDao();
    //查询所有类目信息
    public ArrayList<BasicInfo> findBasicInfo() throws SQLException {
        ArrayList<BasicInfo> basicInfos = basicInfoDao.selectBasicInfo();
        return basicInfos;
    }

    //添加评估类目信息
    public boolean saveBasicInfo(String basicInfoName) throws SQLException {
        int i = basicInfoDao.insertBasicInfo(basicInfoName);
        return i>0;
    }
    

    //停用
    public boolean stop(int basicInfoId) throws SQLException {
        return changeStatusById(0,basicInfoId);

    }

    //启用
    public boolean start(int basicInfoId) throws SQLException {
        return changeStatusById(1,basicInfoId);

    }
    //根据id改变状态
    public boolean changeStatusById(int basicInfoStatus,int basicInfoId) throws SQLException {
        int i = basicInfoDao.changeStatusById( basicInfoStatus,basicInfoId);
        boolean b = i>0;
        return b;
    }

    //根据id删除信息
    public boolean deleteBasicInfoById(int basicInfoId) throws SQLException {
        InfoDetailDao infoDetailDao = new InfoDetailDao();

        ArrayList<InfoDetail> infoDetails = infoDetailDao.selectInfoDetailByBasicInfoId(basicInfoId);
        if(infoDetails.size() > 0){
            return false;
        }else {
            int i = basicInfoDao.DeleteBasicInfoById(basicInfoId);
            return i>0;
        }

    }

    //根据id修改类目信息
    public boolean updateBasicInfoById(String basicInfoName,int basicInfoId) throws SQLException {
        int i = basicInfoDao.updateBasicInfoById(basicInfoName, basicInfoId);
        return i>0;
    }
    //根据id查询类目信息
    public BasicInfo findBasicInfoById(int basicInfoId) throws SQLException {
        BasicInfo basicInfo = basicInfoDao.selectBasicInfoById(basicInfoId);
        return basicInfo;
    }
}
